'use client'; import { useEffect, useRef, useState } from 'react'; import { X } from 'lucide-react'; import { fetchApi } from '@/lib/utils/client'; import useAuth from '@/hooks/useAuth'; import EmojiPicker from '@/app/component/EmojiPicker'; import { FeedReply } from '@/types/feed/post'; const MAX_LENGTH = 300; type Props = { postID: number; replyTarget: FeedReply|null; onClearReplyTarget: () => void; onSubmitted: () => void; }; export default function FeedReplyComposer({ postID, replyTarget, onClearReplyTarget, onSubmitted }: Props) { const { member, loginCheck } = useAuth(); const [content, setContent] = useState(''); const [submitting, setSubmitting] = useState(false); const [error, setError] = useState(null); const textareaRef = useRef(null); const isOver = content.length > MAX_LENGTH; useEffect(() => { if (replyTarget) { textareaRef.current?.focus(); } }, [replyTarget]); const handleEmojiSelect = (emoji: string) => { const textarea = textareaRef.current; if (!textarea) { setContent((prev) => prev + emoji); return; } const start = textarea.selectionStart ?? content.length; const end = textarea.selectionEnd ?? content.length; const next = content.slice(0, start) + emoji + content.slice(end); setContent(next); requestAnimationFrame(() => { textarea.focus(); const caret = start + emoji.length; textarea.setSelectionRange(caret, caret); }); }; const handleSubmit = async () => { if (!loginCheck() || submitting) { return; } const trimmed = content.trim(); if (trimmed.length === 0) { setError('답글 내용을 입력해주세요.'); return; } if (isOver) { setError(`답글은 최대 ${MAX_LENGTH}자까지 작성할 수 있습니다.`); return; } setSubmitting(true); setError(null); try { const body: { content: string; parentCommentID?: number } = { content: trimmed }; if (replyTarget) { body.parentCommentID = replyTarget.id; } const res = await fetchApi(`/api/feed/post/${postID}/comment`, { method: 'POST', body, silent: true }); if (!res.success) { setError(res.message ?? '댓글 등록 실패'); return; } setContent(''); onClearReplyTarget(); onSubmitted(); } catch (err) { console.error(err); setError(err instanceof Error ? err.message : '답글 등록 실패'); } finally { setSubmitting(false); } }; if (!member) { return (

답글을 남기려면 로그인이 필요합니다.

); } return (
{member.thumb ? ( {member.name ) : ( {(member.name ?? member.sid).charAt(0).toUpperCase()} )}
{replyTarget && (
{(replyTarget.authorName || replyTarget.authorSID) ?? '알 수 없음'}님에게 답글
)}